<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>贪吃蛇游戏</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #000;
            color: #fff;
            font-family: Arial, sans-serif;
            flex-direction: column;
        }
        canvas {
            border: 1px solid #fff;
            display: none;
        }
        .controls {
            display: none;
            justify-content: center;
            align-items: center;
            margin-top: 20px;
            flex-direction: column;
        }
        .controls button {
            width: 50px;
            height: 50px;
            margin: 5px;
            font-size: 20px;
            background-color: #333;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        .controls .row {
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .start-screen {
            display: flex;
            flex-direction: column;
            align-items: center;
        }
        .start-screen button {
            padding: 10px 20px;
            font-size: 20px;
            background-color: #333;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="start-screen">
        <h1>贪吃蛇游戏</h1>
        <button id="startBtn">开始游戏</button>
    </div>
    <canvas id="gameCanvas" width="400" height="400"></canvas>
    <div class="controls">
        <div class="row">
            <button id="upBtn">↑</button>
        </div>
        <div class="row">
            <button id="leftBtn">←</button>
            <button id="downBtn">↓</button>
            <button id="rightBtn">→</button>
        </div>
    </div>

    <!-- 添加背景音乐 -->
    <audio id="backgroundMusic" loop>
        <source src="背景音乐_爱给网_aigei_com.mp3" type="audio/mpeg">
        您的浏览器不支持 audio 元素。
    </audio>

    <script>
        const canvas = document.getElementById('gameCanvas');
        const ctx = canvas.getContext('2d');

        const gridSize = 20;
        const tileCount = canvas.width / gridSize;

        let snake = [{ x: 10, y: 10 }];
        let food = { x: 15, y: 15 };
        let direction = { x: 0, y: 0 };
        let score = 0;
        let gameLoop;

        // 加载苹果图片
        const appleImage = new Image();
        appleImage.src = 'apple.png'; // 请确保图片文件在同一目录下

        function draw() {
            ctx.fillStyle = '#000';
            ctx.fillRect(0, 0, canvas.width, canvas.height);

            ctx.fillStyle = '#0f0';
            snake.forEach(segment => ctx.fillRect(segment.x * gridSize, segment.y * gridSize, gridSize, gridSize));

            // 绘制苹果
            ctx.drawImage(appleImage, food.x * gridSize, food.y * gridSize, gridSize, gridSize);

            ctx.fillStyle = '#fff';
            ctx.fillText('得分: ' + score, 10, 20);
        }

        function update() {
            const head = { x: snake[0].x + direction.x, y: snake[0].y + direction.y };
            snake.unshift(head);

            if (head.x === food.x && head.y === food.y) {
                score++;
                food = { x: Math.floor(Math.random() * tileCount), y: Math.floor(Math.random() * tileCount) };
            } else {
                snake.pop();
            }

            if (head.x < 0 || head.x >= tileCount || head.y < 0 || head.y >= tileCount || snake.slice(1).some(segment => segment.x === head.x && segment.y === head.y)) {
                clearInterval(gameLoop);
                alert('游戏结束！得分: ' + score);
                location.reload(); // 游戏结束后自动刷新页面
            }
        }

        document.addEventListener('keydown', (event) => {
            switch (event.key) {
                case 'ArrowUp':
                    if (direction.y === 0) direction = { x: 0, y: -1 };
                    break;
                case 'ArrowDown':
                    if (direction.y === 0) direction = { x: 0, y: 1 };
                    break;
                case 'ArrowLeft':
                    if (direction.x === 0) direction = { x: -1, y: 0 };
                    break;
                case 'ArrowRight':
                    if (direction.x === 0) direction = { x: 1, y: 0 };
                    break;
            }
        });

        const upBtn = document.getElementById('upBtn');
        const downBtn = document.getElementById('downBtn');
        const leftBtn = document.getElementById('leftBtn');
        const rightBtn = document.getElementById('rightBtn');

        upBtn.addEventListener('click', () => {
            if (direction.y === 0) direction = { x: 0, y: -1 };
        });

        downBtn.addEventListener('click', () => {
            if (direction.y === 0) direction = { x: 0, y: 1 };
        });

        leftBtn.addEventListener('click', () => {
            if (direction.x === 0) direction = { x: -1, y: 0 };
        });

        rightBtn.addEventListener('click', () => {
            if (direction.x === 0) direction = { x: 1, y: 0 };
        });

        const startBtn = document.getElementById('startBtn');
        const backgroundMusic = document.getElementById('backgroundMusic');

        startBtn.addEventListener('click', () => {
            document.querySelector('.start-screen').style.display = 'none';
            canvas.style.display = 'block';
            document.querySelector('.controls').style.display = 'flex';
            gameLoop = setInterval(() => {
                update();
                draw();
            }, 100);

            // 开始播放背景音乐
            backgroundMusic.play();
        });
    </script>
</body>
</html>
